package com.zengguang.testkaoshi.controller;

import com.zengguang.testkaoshi.pojo.TestDataBaseInfo;
import com.zengguang.testkaoshi.services.impl.TestDataBaseInfoService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "TestDataBaseInfo")
@RestController
@CrossOrigin
public class TestDataBaseInfoAction {
    @Autowired
    private TestDataBaseInfoService testDataBaseInfoService;

    @ApiOperation(value = "题库信息添加方法")
    @RequestMapping(value = "/addTestDataBaseInfo",method = RequestMethod.GET)
    public ResponseMessage addTestDataBaseInfo(TestDataBaseInfo testDataBaseInfo) {
        int isAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (testDataBaseInfo != null) {
            //添加角色数据
            isAdd = testDataBaseInfoService.insertTestDataBase(testDataBaseInfo);
            if (isAdd == 1) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        }
        return Result.error("添加失败");
    }

    @ApiOperation(value = "题库信息修改方法")
    @RequestMapping(value = "/updateTestDataBaseInfo",method = RequestMethod.GET)
    public ResponseMessage updateTestDataBaseInfo(TestDataBaseInfo testDataBaseInfo) {
        int isUpadte = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (testDataBaseInfo != null) {
            //修改题库信息数据
            isUpadte = testDataBaseInfoService.updateTestDataBase(testDataBaseInfo);
            if (isUpadte >0) {
                return Result.success("题库信息更新成功");
            } else {
                return Result.error("题库信息更新失败");
            }

        }
        return Result.error("题库信息更新失败");
    }

    @ApiOperation(value = "题库信息信息查询方法")
    @RequestMapping(value = "/selectTestDataBaseInfo",method = RequestMethod.GET)
    public ResponseMessage selectTestDataBaseInfo(TestDataBaseInfo testDataBaseInfo) {
        Map<String, List> map=new HashMap<>();
        List<TestDataBaseInfo> list=testDataBaseInfoService.selectAllTestDataBase();
        if (!list.isEmpty()){
            map.put("list",list);
            return Result.success("200",map);
        }
        return Result.error("查询失败");
    }

    @ApiOperation("删除题库信息")
    @RequestMapping(value = "/deleteTestDataBaseInfo",method = RequestMethod.GET)
    public ResponseMessage deleteTestDataBaseInfo(int testdatabaseinfoid){
        int temp=0;
        if (testdatabaseinfoid!=0){
            temp=testDataBaseInfoService.deleteTestDataBase(testdatabaseinfoid);
            if (temp>0) {
                return Result.success("删除题库信息成功",temp);
            }
            else {
                return Result.error("删除题库信息失败");
            }
        }

        return Result.error("题库信息编号不为0");
    }
}
